/*
 * @Author: Cenven
 * @Date: 2020-12-07 11:07:37
 * @Description: 北京泓深教育发展有限公司
 * @Descripttion:
 * @version: @Lastest
 * @LastEditTime: 2021-01-20 16:03:37
 * @CopyRight: 北京泓深教育发展有限公司
 */
export const scroll = (top_far, scroll_target) => {
  if (!scroll_target) {
    scroll_target = window;
  }
  if (!window.scrollTo) {
    window.scrollTo = function(x, y) {
      window.pageXOffset = x;
      window.pageYOffset = y;
    };
  }
  if (!document.body.scrollTo) {
    Element.prototype.scrollTo = function(x, y) {
      this.scrollLeft = x;
      this.scrollTop = y;
    };
  }
  top_far = Math.ceil(top_far);
  let current_scroll_top =
    document.documentElement.scrollTop ||
    document.body.scrollTop ||
    window.pageYOffset;
  if (scroll_target) {
    current_scroll_top = scroll_target.scrollTop;
  }
  current_scroll_top = Math.ceil(current_scroll_top);
  let distant = current_scroll_top;
  //距离分成65份，定时器每10毫秒增加一份距离，使滚动时间可以控制在650毫秒
  let timer = setInterval(() => {
    if (distant < top_far) {
      const speed = Math.ceil((top_far - current_scroll_top) / 65);
      distant += speed;
      if (distant >= top_far) {
        scroll_target.scrollTo(0, top_far);
        return clearInterval(timer);
      }
      scroll_target.scrollTo(0, Math.ceil(distant));
    } else if (distant > top_far) {
      const speed = Math.ceil((current_scroll_top - top_far) / 65);
      distant -= speed;
      if (distant <= top_far) {
        scroll_target.scrollTo(0, top_far);
        return clearInterval(timer);
      }
      scroll_target.scrollTo(0, Math.ceil(distant));
    } else {
      return clearInterval(timer);
    }
  }, 10);
};
export const draggable = (target, callback) => {
  const { start, moving } = callback;
  function touchStart(e) {
    const { pageX, pageY } = e.targetTouches[0];
    let object = start && start();
    document.addEventListener("touchmove", touchMove, false);
    document.addEventListener("touchend", touchEnd, false);
    function touchMove(event) {
      let moveingX = event.targetTouches[0].pageX;
      let moveingY = event.targetTouches[0].pageY;
      let movedX = moveingX - pageX;
      let movedY = moveingY - pageY;
      moving &&
        moving({
          x: movedX,
          y: movedY,
          params: object
        });
    }
    function touchEnd() {
      document.removeEventListener("touchmove", touchMove);
    }
  }
  target.addEventListener("touchstart", touchStart, false);
};
export const uuid2 = (len, radix) => {
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(
    ""
  );
  var uuid = [],
    i;
  radix = radix || chars.length;
  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
  } else {
    var r;
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
    uuid[14] = "4";
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16);
        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
      }
    }
  }
  return uuid.join("");
};
//十六进制转字符串
export const hexCharCodeToStr = hexCharCodeStr => {
  var trimedStr = hexCharCodeStr.trim();
  var rawStr =
    trimedStr.substr(0, 2).toLowerCase() === "0x"
      ? trimedStr.substr(2)
      : trimedStr;
  var len = rawStr.length;
  if (len % 2 !== 0) {
    alert("Illegal Format ASCII Code!");
    return "";
  }
  var curCharCode;
  var resultStr = [];
  for (var i = 0; i < len; i = i + 2) {
    curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
    resultStr.push(String.fromCharCode(curCharCode));
  }
  return resultStr.join("");
};
// 字符串转十六进制
export const strToHexCharCode = str => {
  if (str === "") return "";
  var hexCharCode = [];
  hexCharCode.push("0x");
  for (var i = 0; i < str.length; i++) {
    hexCharCode.push(str.charCodeAt(i).toString(16));
  }
  return hexCharCode.join("");
};

export const getMimeType = url => {
  let mime_list = [
    { str: "3gp", reg: /(\.3gp)$/, mime: "video/3gpp" },
    {
      str: "apk",
      reg: /(\.apk)$/,
      mime: "application/vnd.android.package-archive"
    },
    { str: "asf", reg: /(\.asf)$/, mime: "video/x-ms-asf" },
    { str: "avi", reg: /(\.avi)$/, mime: "video/x-msvideo" },
    { str: "bin", reg: /(\.bin)$/, mime: "application/octet-stream" },
    { str: "bmp", reg: /(\.bmp)$/, mime: "image/bmp" },
    { str: "c", reg: /(\.c)$/, mime: "text/plain" },
    { str: "class", reg: /(\.class)$/, mime: "application/octet-stream" },
    { str: "conf", reg: /(\.conf)$/, mime: "text/plain" },
    { str: "cpp", reg: /(\.cpp)$/, mime: "text/plain" },
    { str: "doc", reg: /(\.doc)$/, mime: "application/msword" },
    {
      str: "docx",
      reg: /(\.docx)$/,
      mime:
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    },
    { str: "xls", reg: /(\.xls)$/, mime: "application/vnd.ms-excel" },
    {
      str: "xlsx",
      reg: /(\.xlsx)$/,
      mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    },
    { str: "exe", reg: /(\.exe)$/, mime: "application/octet-stream" },
    { str: "gif", reg: /(\.gif)$/, mime: "image/gif" },
    { str: "gtar", reg: /(\.gtar)$/, mime: "application/x-gtar" },
    { str: "gz", reg: /(\.gz)$/, mime: "application/x-gzip" },
    { str: "h", reg: /(\.h)$/, mime: "text/plain" },
    { str: "htm", reg: /(\.htm)$/, mime: "text/html" },
    { str: "html", reg: /(\.html)$/, mime: "text/html" },
    { str: "jar", reg: /(\.jar)$/, mime: "application/java-archive" },
    { str: "java", reg: /(\.java)$/, mime: "text/plain" },
    { str: "jpeg", reg: /(\.jpeg)$/, mime: "image/jpeg" },
    { str: "jpg", reg: /(\.jpg)$/, mime: "image/jpeg" },
    { str: "js", reg: /(\.js)$/, mime: "application/x-javascript" },
    { str: "log", reg: /(\.log)$/, mime: "text/plain" },
    { str: "m3u", reg: /(\.m3u)$/, mime: "audio/x-mpegurl" },
    { str: "m4a", reg: /(\.m4a)$/, mime: "audio/mp4a-latm" },
    { str: "m4b", reg: /(\.m4b)$/, mime: "audio/mp4a-latm" },
    { str: "m4p", reg: /(\.m4p)$/, mime: "audio/mp4a-latm" },
    { str: "m4u", reg: /(\.m4u)$/, mime: "video/vnd.mpegurl" },
    { str: "m4v", reg: /(\.m4v)$/, mime: "video/x-m4v" },
    { str: "mov", reg: /(\.mov)$/, mime: "video/quicktime" },
    { str: "mp2", reg: /(\.mp2)$/, mime: "audio/x-mpeg" },
    { str: "mp3", reg: /(\.mp3)$/, mime: "audio/x-mpeg" },
    { str: "mp4", reg: /(\.mp4)$/, mime: "video/mp4" },
    { str: "mpc", reg: /(\.mpc)$/, mime: "application/vnd.mpohun.certificate" },
    { str: "mpe", reg: /(\.mpe)$/, mime: "video/mpeg" },
    { str: "mpeg", reg: /(\.mpeg)$/, mime: "video/mpeg" },
    { str: "mpg", reg: /(\.mpg)$/, mime: "video/mpeg" },
    { str: "mpg4", reg: /(\.mpg4)$/, mime: "video/mp4" },
    { str: "mpga", reg: /(\.mpga)$/, mime: "audio/mpeg" },
    { str: "msg", reg: /(\.msg)$/, mime: "application/vnd.ms-outlook" },
    { str: "ogg", reg: /(\.ogg)$/, mime: "audio/ogg" },
    { str: "pdf", reg: /(\.pdf)$/, mime: "application/pdf" },
    { str: "png", reg: /(\.png)$/, mime: "image/png" },
    { str: "pps", reg: /(\.pps)$/, mime: "application/vnd.ms-powerpoint" },
    { str: "ppt", reg: /(\.ppt)$/, mime: "application/vnd.ms-powerpoint" },
    {
      str: "pptx",
      reg: /(\.pptx)$/,
      mime:
        "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    },
    { str: "prop", reg: /(\.prop)$/, mime: "text/plain" },
    { str: "rc", reg: /(\.rc)$/, mime: "text/plain" },
    { str: "rmvb", reg: /(\.rmvb)$/, mime: "audio/x-pn-realaudio" },
    { str: "rtf", reg: /(\.rtf)$/, mime: "application/rtf" },
    { str: "sh", reg: /(\.sh)$/, mime: "text/plain" },
    { str: "tar", reg: /(\.tar)$/, mime: "application/x-tar" },
    { str: "tgz", reg: /(\.tgz)$/, mime: "application/x-compressed" },
    { str: "txt", reg: /(\.txt)$/, mime: "text/plain" },
    { str: "wav", reg: /(\.wav)$/, mime: "audio/x-wav" },
    { str: "wma", reg: /(\.wma)$/, mime: "audio/x-ms-wma" },
    { str: "wmv", reg: /(\.wmv)$/, mime: "audio/x-ms-wmv" },
    { str: "wps", reg: /(\.wps)$/, mime: "application/vnd.ms-works" },
    { str: "xml", reg: /(\.xml)$/, mime: "text/plain" },
    { str: "z", reg: /(\.z)$/, mime: "application/x-compress" },
    { str: "zip", reg: /(\.zip)$/, mime: "application/x-zip-compressed" },
    { str: "", reg: / /, mime: "*/*" }
  ];
  for (let i = 0; i < mime_list.length; i++) {
    if (mime_list[i].reg.test(url)) {
      return mime_list[i];
    }
  }
};

/**
 *
 * @param {*} col 输入颜色得到一个更淡或更浅的颜色
 * @param {*} amt 正数得到更浅的颜色 负数得到更深的颜色
 */
export const LightenDarkenColor = (col, amt) => {
  var usePound = false;

  if (col[0] == "#") {
    col = col.slice(1);
    usePound = true;
  }

  var num = parseInt(col, 16);

  var r = (num >> 16) + amt;

  if (r > 255) r = 255;
  else if (r < 0) r = 0;

  var b = ((num >> 8) & 0x00ff) + amt;

  if (b > 255) b = 255;
  else if (b < 0) b = 0;

  var g = (num & 0x0000ff) + amt;

  if (g > 255) g = 255;
  else if (g < 0) g = 0;

  return (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
};
